// /*================================================================
// *   Copyright (C) 2023 baichao All rights reserved.
// *
// *   文件名称：sys_brk_demo.c
// *   创 建 者：baichao
// *   创建日期：2023年08月01日
// *   描    述：
// *
// ================================================================*/

#include <unistd.h>
#include <stdio.h>
#include <sys/syscall.h> // 增加此头文件

int main()
{
    void *cur_brk;

    // 获取当前程序数据段结束地址
    long ret = syscall(__NR_brk, 0);
    cur_brk = (void *)ret;
    printf("start, curr data sec end pos: %p\n", cur_brk);

    // 增加堆的大小
    long delta = 1024;
    ret = syscall(__NR_brk, cur_brk + delta); // 堆增长delta
    cur_brk = (void *)ret;
    printf("add, curr data sec end pos: %p\n", cur_brk);

    // 减小堆的大小
    delta = -512;
    ret = syscall(__NR_brk, cur_brk + delta); // 堆缩小delta
    cur_brk = (void *)ret;
    printf("sub, curr data sec end pos: %p\n", cur_brk);

    // 获取当前程序数据段结束地址
    ret = syscall(__NR_brk, 0);
    cur_brk = (void *)ret;
    printf("end, curr data sec end pos: %p\n", cur_brk);

    return 0;
}
